﻿using System;
using System.Configuration;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FunctionalCLR.Core;

namespace Zove.Desktop {
    public partial class PreferencesForm : Form {

        public void InitProgrammingTab() {
            this.tbPrecision.Text = Utils.Precision.ToString();

            string[] lines = System.IO.File.ReadAllLines(
                Utils.ConstantFile, Encoding.UTF8);
            foreach (string ln in lines) {
                string[] p = ln.Split('=');
                gvConstants.Rows.Add(p[0], p[1]);
            }

            string[] classPath = Utils.FunctionPath.Split(';');
            foreach (string cp in classPath) {
                this.gvFunctionLibraries.Rows.Add(cp);
            }
        }

        public void ApplyProgrammingTabSettings() {
            if (Convert.ToInt32(
                this.tbPrecision.Text) < 0) {
                MessageBox.Show("Precision cannot be negatvie.");
                return;
            }

            ConfigurationManager.AppSettings["precision"]
                = this.tbPrecision.Text;

            StringBuilder path = new StringBuilder();

            foreach (DataGridViewRow r in this.gvFunctionLibraries.Rows) {
                path.Append(r.Cells["ColLibraryPath"].Value).Append(";");
            }
            Utils.ModelPath = path.ToString();

            path = new StringBuilder();
            foreach (DataGridViewRow r in gvConstants.Rows) {
                if (r.Cells["colName"].Value == null) {
                    continue;
                }

                path.AppendLine(r.Cells["colName"].Value + "=" + r.Cells["colValue"].Value);
            }
            System.IO.File.WriteAllText(Utils.ConstantFile, path.ToString());

            path = new StringBuilder();
            foreach (DataGridViewRow r in this.gvFunctionLibraries.Rows) {
                if (r.Cells["ColLibraryName"].Value == null) {
                    continue;
                }

                path.AppendLine(r.Cells["ColLibraryPath"].Value.ToString()).Append(";");
            }
            path.Remove(path.Length - 1, 1);
            Utils.FunctionPath = path.ToString();
        }

        public PreferencesForm() {
            InitializeComponent();

            this.InitProgrammingTab();
            cbLN.Checked = DesktopFormContext.Context.ShowLineNumber;
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e) {
        }

        private void button1_Click(object sender, EventArgs e) {
            button2_Click(sender, e);
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e) {
            try {
                this.ApplyProgrammingTabSettings();

                if (font != null) {
                    UpdateFontSettings(font);
                    DesktopFormContext.Context.Font = font;
                }

                if (this.cbLN.Checked 
                    != DesktopFormContext.Context.ShowLineNumber) {
                    UpdateShowLineNumber(this.cbLN.Checked);
                    DesktopFormContext.Context.ShowLineNumber 
                        = this.cbLN.Checked;
                }
            } catch (Exception ex) { 
                MessageBox.Show(ex.Message); 
            }
        }

        private Font font = null;

        private void button3_Click(object sender, EventArgs e) {
            FontDialog d = new FontDialog();
            d.Font = DesktopFormContext.Context.Font;

            if (d.ShowDialog() == DialogResult.OK) {
                font = d.Font;
            }
        }

        public void UpdateFontSettings(Font font) {
            foreach (Form f in DesktopFormContext.Context.Values) {
                if (f.GetType().Equals(typeof(EditingForm))) {
                    EditingForm ef = (EditingForm)f;

                    ef.Font = font;
                }
            }
        }

        public void UpdateShowLineNumber(bool show) {
            foreach (Form f in DesktopFormContext.Context.Values) {
                if (f.GetType().Equals(typeof(EditingForm))) {
                    EditingForm ef = (EditingForm)f;

                    ef.LineNumberVisible = show;
                }
            }
        }
    }
}
